#!/bin/bash
function build_one
{
SYSTEMROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
TOOLCHAIN_PREFIX=$NDK/toolchains/llvm/prebuilt/linux-x86_64
CXX=$TOOLCHAIN_PREFIX/bin/$ARCH_TYPE$API-clang++
CC=$TOOLCHAIN_PREFIX/bin/$ARCH_TYPE$API-clang
echo "######start to configure ffmpeg"
./configure \
--prefix=$PREFIX \
--arch=$ARCH \
--target-os=linux \
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN_PREFIX/bin/arm-linux-androideabi- \
--sysroot=$SYSTEMROOT \
--cxx=$CXX \
--cc=$CC \
--nm=$TOOLCHAIN_PREFIX/bin/arm-linux-androideabi-nm \
--extra-cxxflags="-D__thumb__ -fexceptions -frtti" \
--extra-cflags="$CFLAGS -Os -fPIC -DANDROID -Wfatal-errors -Wno-deprecated -I${EX_LIB}/include" \
--extra-ldflags="-L$SYSTEMROOT/usr/lib -L${EX_LIB}/lib" \
--pkg-config="pkg-config --shared" \
--disable-static \
--enable-shared \
--enable-gpl \
--enable-version3 \
--enable-runtime-cpudetect \
--enable-small \
--enable-network \
--disable-iconv \
--enable-asm \
--enable-neon \
--enable-yasm \
--disable-encoders \
--enable-encoder=h263 \
--enable-encoder=libx264 \
--enable-encoder=aac \
--enable-encoder=mpeg4 \
--enable-encoder=mjpeg \
--enable-encoder=png \
--enable-encoder=gif \
--enable-encoder=bmp \
--disable-muxers \
--enable-muxer=h264 \
--enable-muxer=flv \
--enable-muxer=gif \
--enable-muxer=mp3 \
--enable-muxer=dts \
--enable-muxer=mp4 \
--enable-muxer=mov \
--enable-muxer=mpegts \
--disable-decoders \
--enable-decoder=aac \
--enable-decoder=aac_latm \
--enable-decoder=mp3 \
--enable-decoder=h263 \
--enable-decoder=h264 \
--enable-decoder=mpeg4 \
--enable-decoder=mjpeg \
--enable-decoder=gif \
--enable-decoder=png \
--enable-decoder=bmp \
--enable-decoder=yuv4 \
--disable-demuxers \
--enable-demuxer=image2 \
--enable-demuxer=h263 \
--enable-demuxer=h264 \
--enable-demuxer=flv \
--enable-demuxer=gif \
--enable-demuxer=aac \
--enable-demuxer=ogg \
--enable-demuxer=dts \
--enable-demuxer=mp3 \
--enable-demuxer=mov \
--enable-demuxer=m4v \
--enable-demuxer=concat \
--enable-demuxer=mpegts \
--enable-demuxer=mjpeg \
--enable-demuxer=mpegvideo \
--enable-demuxer=rawvideo \
--enable-demuxer=yuv4mpegpipe \
--enable-demuxer=rtsp \
--disable-parsers \
--enable-parser=aac \
--enable-parser=ac3 \
--enable-parser=h264 \
--enable-parser=mjpeg \
--enable-parser=png \
--enable-parser=bmp\
--enable-parser=mpegvideo \
--enable-parser=mpegaudio \
--disable-protocols \
--enable-protocol=file \
--enable-protocol=hls \
--enable-protocol=concat \
--enable-protocol=rtp \
--enable-protocol=rtmp \
--enable-protocol=rtmpt \
--disable-filters \
--disable-filters \
--enable-filter=aresample \
--enable-filter=asetpts \
--enable-filter=setpts \
--enable-filter=ass \
--enable-filter=scale \
--enable-filter=concat \
--enable-filter=atempo \
--enable-filter=movie \
--enable-filter=overlay \
--enable-filter=rotate \
--enable-filter=transpose \
--enable-filter=hflip \
--disable-outdevs \
--disable-doc \
--disable-ffplay \
--disable-ffmpeg \
--disable-debug \
--disable-ffprobe \
--disable-postproc \
--enable-avdevice \
--disable-symver \
--enable-zlib \
--enable-libx264 \
--disable-stripping

echo "######start making $CPU"

make clean
make -j8
make install
echo "######finish making $CPU"
}

cd ..
WORK_DIR=$(pwd)
# 设置构建目录 build
BUILD_DIR=${WORK_DIR}/build
# 判断 build 文件夹是否存在
if [ ! -d ${BUILD_DIR} ]; then
    mkdir -p ${BUILD_DIR}
fi

# ndk 主目录
NDK=$BUILD_DIR/android-ndk-r21e
# ffmpeg 主目录
FFMPEG_DIR=$BUILD_DIR/ffmpeg

API=21
# arm-v7a

cd $FFMPEG_DIR
CPU=armv7-a
ARCH=arm
ARCH_TYPE=armv7a-linux-androideabi
CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon"
# 设置 lib 目录
PREFIX=${BUILD_DIR}/android/${CPU}/$API
# ${BUILD_DIR}/android/armeabi-v7a
EX_LIB=${BUILD_DIR}/android/armeabi-v7a
 # 设置动态库路径
 export PKG_CONFIG_PATH=${EX_LIB}/lib/pkgconfig
build_one
